﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public List<int> A { get; set; }


        public List<Tuple<int,int>> R1 { get; set; }
        public List<Tuple<int, int>> R2 { get; set; }

        public Form1()
        {
            InitializeComponent();
        }

        void SetAR()
        {
            A = this.textBox1.Text.Split(",").Select(s => int.Parse(s)).ToList();
            List<string> rs = this.textBox2.Text.Split("),").Select(s => s.TrimStart('(').TrimEnd(')')).ToList();

            R1 = new List<Tuple<int, int>>();
            R2 = new List<Tuple<int, int>>();
            rs.ForEach(s =>
            {
                string[] ab = s.Split(",");
                R1.Add(new Tuple<int, int>(int.Parse(ab[0]), int.Parse(ab[1])));
                R2.Add(new Tuple<int, int>(int.Parse(ab[0]), int.Parse(ab[1])));
            });
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SetAR();

            List<Tuple<int, int>> RoR = new List<Tuple<int, int>>();

            R1.ForEach(r =>
            {
                RoR.AddRange((from a in R2
                              where a.Item1 == r.Item2
                              select new Tuple<int, int>(r.Item1, a.Item2)));
            });

            List<string> list = new List<string>();
            RoR.ForEach(r =>
            {
                list.Add(string.Format("({0},{1})", r.Item1, r.Item2));
            });

            list = list.Distinct().ToList();

            this.richTextBox1.Text = "RoR:{" + string.Join(",", list) + "},共" + list.Count + "个元素";

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SetAR();

            List<Tuple<int, int>> rR = R1.Select(s => s).ToList();

            rR.AddRange((from a in A
                         select new Tuple<int, int>(a, a)));

            List<string> list = new List<string>();

            rR.ForEach(r =>
            {
                list.Add(string.Format("({0},{1})", r.Item1, r.Item2));
            });

            list = list.Distinct().ToList();

            this.richTextBox1.Text = "r(R):{" + string.Join(",", list) + "},共" + list.Count + "个元素";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SetAR();

            List<Tuple<int, int>> sR = R1.Select(s => s).ToList();

            R1.ForEach(r =>
            {
                sR.Add(new Tuple<int, int>(r.Item2, r.Item1));
            });

            List<string> list = new List<string>();

            sR.ForEach(r =>
            {
                list.Add(string.Format("({0},{1})", r.Item1, r.Item2));
            });

            list = list.Distinct().ToList();

            this.richTextBox1.Text = "s(R):{" + string.Join(",", list) + "},共" + list.Count + "个元素";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SetAR();

            List<Tuple<int, int>> RoR = new List<Tuple<int, int>>();

            R1.ForEach(r =>
            {
                RoR.AddRange((from a in R2
                              where a.Item1 == r.Item2
                              select new Tuple<int, int>(r.Item1, a.Item2)));
            });

            List<string> list = new List<string>();

            R1.ForEach(r =>
            {
                list.Add(string.Format("({0},{1})", r.Item1, r.Item2));
            });

            RoR.ForEach(r =>
            {
                list.Add(string.Format("({0},{1})", r.Item1, r.Item2));
            });

            list = list.Distinct().ToList();

            this.richTextBox1.Text = "t(R):{" + string.Join(",", list) + "},共" + list.Count + "个元素";
        }
    }
}
